package com.asset.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * BigDecimal 类 JSON 序列化配置
 * @author 11725
 */
public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
    @Override
    public void serialize(BigDecimal value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 保留两位小数
        if (Objects.isNull(value)) {
            jsonGenerator.writeNull();
        } else {
            jsonGenerator.writeString(value.setScale(4, RoundingMode.FLOOR) + "");
        }
    }
}
